#ifndef __GLOBAL_CONFIG_H__
#define __GLOBAL_CONFIG_H__

#include "common.h"

/* System configuration structure */
typedef struct {
    uint32_t version;
    uint32_t magic;
    uint32_t crc32;
} system_config_t;

/* IO module configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        rt_bool_t normally_open;
        uint32_t filter_time;
        rt_bool_t current_state;
        rt_bool_t valid_state;
        uint8_t debounce_count;
        uint8_t debounce_threshold;
    } inputs[12];
    
    struct {
        rt_bool_t normally_open;
        rt_bool_t command_state;
        rt_bool_t current_state;
        rt_bool_t safe_state;
        uint32_t on_delay;
        uint32_t off_delay;
    } outputs[12];
} io_config_t;

/* ADC module configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        float min_current;    // 4mA
        float max_current;    // 20mA
        float min_engineering; // Engineering value at 4mA
        float max_engineering; // Engineering value at 20mA
        float current_value;
        float engineering_value;
        uint16_t adc_raw;
        uint8_t filter_count;
        float filter_buffer[10];
    } channels[4];
} adc_config_t;

/* Temperature sensor configuration */
typedef enum {
    SENSOR_PT100 = 0,
    SENSOR_NTC10K,
    SENSOR_THERMOCOUPLE
} sensor_type_t;

typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        sensor_type_t type;
        uint32_t filter_time;
        float current_temp;
        float k_factor;
        float b_offset;
        cal_point_t cal_table[4];
        uint8_t cal_table_size;
        uint32_t last_read_time;
    } channels[4];
} temp_config_t;

/* Flow sensor configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        uint32_t pulse_count;
        uint32_t last_pulse_count;
        float pulse_constant;  // pulses per liter
        float flow_rate;
        float total_flow;
        float k_factor;
        uint32_t filter_time;
        uint32_t last_calc_time;
    } channels[2];
} flow_config_t;

/* Water quality configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        float resistance;
        float quality_value;
        float k_factor;
        float b_offset;
        cal_point_t cal_table[4];
        uint8_t cal_table_size;
        uint32_t filter_time;
    } channels[2];
} water_config_t;

/* Valve configuration */
typedef struct {
    config_base_t base;
    
    rt_bool_t enabled;
    uint32_t max_steps;
    uint32_t current_step;
    uint32_t step_delay;
    uint8_t step_sequence[8];
    uint8_t current_phase;
    float position_percent;
    rt_bool_t moving;
    uint32_t target_step;
} valve_config_t;

/* PID configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        rt_bool_t auto_mode;
        float kp;
        float ki;
        float kd;
        float setpoint;
        float input;
        float output;
        float integral;
        float prev_error;
        float output_min;
        float output_max;
        uint32_t control_interval;
        uint32_t last_control_time;
    } controllers[2];  // 0: heater, 1: compressor
} pid_config_t;

/* Alarm configuration */
typedef struct {
    config_base_t base;
    
    struct {
        rt_bool_t enabled;
        float low_limit;
        float high_limit;
        float deadband;
        alarm_level_t level;
        rt_bool_t triggered;
        uint32_t trigger_time;
        uint32_t acknowledge_time;
    } alarms[10];
} alarm_config_t;

/* Log configuration */
typedef struct {
    config_base_t base;
    
    uint32_t max_entries;
    uint32_t current_index;
    uint32_t wrap_around;
    rt_bool_t enable_console;
    rt_bool_t enable_storage;
    uint32_t storage_interval;
} log_config_t;

/* Complete system configuration */
typedef struct {
    system_config_t system;
    io_config_t io;
    adc_config_t adc;
    temp_config_t temp;
    flow_config_t flow;
    water_config_t water;
    valve_config_t valve;
    pid_config_t pid;
    alarm_config_t alarm;
    log_config_t log;
} system_config_complete_t;

/* Global configuration instance */
extern system_config_complete_t g_system_config;

/* Function declarations */
rt_err_t config_init(void);
rt_err_t config_load(void);
rt_err_t config_save(void);
rt_err_t config_validate(system_config_complete_t *config);
uint32_t config_calculate_crc(system_config_complete_t *config);

#endif /* __GLOBAL_CONFIG_H__ */
